/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.prodcuct.service;

import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.anny.prodcuct.entity.CategoryResponseBody;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.anny.prodcuct.entity.ProductCategory;
import com.jeeplus.modules.anny.prodcuct.mapper.ProductCategoryMapper;

/**
 * 应用于商品分类的类别表Service
 * @author zhengcq
 * @version 2018-10-17
 */
@Service
@Transactional(readOnly = true)
public class ProductCategoryService extends CrudService<ProductCategoryMapper, ProductCategory> {

	public ProductCategory get(String id) {
		return super.get(id);
	}
	
	public List<ProductCategory> findList(ProductCategory productCategory) {
		if (StringUtils.isBlank(productCategory.getParentId()) ){
			productCategory.setParentId("1");
		}
		return super.findList(productCategory);
	}
	
	public Page<ProductCategory> findPage(Page<ProductCategory> page, ProductCategory productCategory) {
		if (StringUtils.isBlank(productCategory.getParentId())  ){
			productCategory.setParentId("1");
		}
		return super.findPage(page, productCategory);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductCategory productCategory) {
		if (StringUtils.isBlank(productCategory.getParentId())  ){
			productCategory.setParentId("1");
			productCategory.setLevel("1");
		}else {
			if (!"1".equals(productCategory.getParentId())){
				productCategory.setType(this.get(productCategory.getParentId()).getType());
				productCategory.setLevel("2");
			}
		}
		if (productCategory.getIsNewRecord()){
			productCategory.preInsert();
			mapper.insert(productCategory);
		}else{
			productCategory.preUpdate();
			mapper.update(productCategory);
			ProductCategory p = new ProductCategory();
			p.setLevel("2");
			p.setParentId(productCategory.getId());
			List<ProductCategory> list = this.findList(p);
			if (list != null && list.size() >0){
				for (ProductCategory pro : list){
					pro.setType(productCategory.getType());
					mapper.update(pro);
				}
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ProductCategory productCategory) {
		ProductCategory p = new ProductCategory();
		p.setParentId(productCategory.getId());
		List<ProductCategory> list = this.findList(p);
		if(list != null && list.size() > 0){
			for (ProductCategory pc : list){
				super.delete(pc);
			}
		}
		super.delete(productCategory);
	}

	public List<CategoryResponseBody> getCategory(ProductCategory productCategory){
		List<CategoryResponseBody> list = new ArrayList<>();
		ProductCategory p = new ProductCategory();
		p.setLevel("1");
		p.setType(productCategory.getType());
		List<ProductCategory> productCategories = this.mapper.findList(p);
		if (productCategories != null && productCategories.size() >0){
			for(ProductCategory pro:productCategories){
				CategoryResponseBody categoryResponseBody = new CategoryResponseBody();
				categoryResponseBody.setId(pro.getId());
				categoryResponseBody.setName(pro.getName());
				ProductCategory pc = new ProductCategory();
				pc.setType(productCategory.getType());
				pc.setParentId(pro.getId());
				pc.setLevel("2");
				categoryResponseBody.setCategories(this.mapper.findList(pc));
				categoryResponseBody.setType(pro.getType());
				list.add(categoryResponseBody);
			}
		}
		return list;
	}
	
}